package com.brillilab.service.logic.inventory;

import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.domain.po.inventory.InventoryShare;
import com.brillilab.domain.po.inventory.ReagentInventory;
import com.brillilab.domain.po.lab.LabMember;
import com.brillilab.domain.vo.space.InventoryShareAddVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.inventory.IInventoryShareService;
import com.brillilab.service.core.inventory.IReagentInventoryService;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.core.system.IDictService;
import com.brillilab.service.module.message.producer.MessageProducer;
import com.brillilab.service.module.message.template.impl.reagent.ReagentShareTemp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wuzhiyu
 * @Title: InventoryShareLogic
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2018/11/2116:07
 */
@Service
@Transactional
public class InventoryShareLogic {
    @Autowired
    private ILabMemberService labMemberService;
    @Autowired
    private IReagentInventoryService reagentInventoryService;
    @Autowired
    private IInventoryShareService inventoryShareService;
    @Resource
    private MessageProducer messageProducer;
    @Resource
    private IDictService dictService;

    public void InventoryShare(InventoryShareAddVo shareAddVo, Long labMemberId, UserInfoVo userInfo) {
        LabMember me = labMemberService.selectById(labMemberId);
        List<InventoryShare> shareList = new ArrayList<>();
        shareAddVo.getInventoryIdList().forEach(i -> {
            ReagentInventory inv = reagentInventoryService.selectById(i);
            Assert.notNull(inv, "inventory not exist");
            if ((!me.getLabId().equals(inv.getLabId())) || (!me.getId().equals(inv.getLabMemberId()))) {
                throw new BrillilabException(ResultEnum.USER_NOT_POWER);
            }
            shareAddVo.getLabMemberIdList().forEach(m -> {
                LabMember labMember = labMemberService.selectById(m);
                Assert.notNull(labMember, "labMember not exist");
                if (!me.getLabId().equals(labMember.getLabId())) {
                    throw new BrillilabException(ResultEnum.USER_NOT_POWER);
                }
                InventoryShare share = new InventoryShare();
                share.setState(0);
                share.setInventoryId(i);
                share.setFromLabMemberId(me.getId());
                share.setReagentId(inv.getReagentId());
                share.setKitId(inv.getKitId());
                share.setToLabMemberId(m);
                shareList.add(share);
            });
        });
        Integer result = inventoryShareService.addList(shareList);
        if (result > 0) {
            //试剂分享后，执行消息推送
            shareList.forEach(m -> {
                //分享目标（接受人）
                LabMember toLabMember = labMemberService.selectById(m.getToLabMemberId());
                //获取试剂信息
                ReagentInventory inv = reagentInventoryService.selectById(m.getInventoryId());
                //存储路径（设备（冰箱）-->平面-->试剂盒-->试剂）
                String savePath = "";
                Map<Integer, String> zoneMap = dictService.getZoneMap();//温区集合
                //判断试剂盒是否为空且大于0,是则显示试剂盒+试剂信息，否则不显示
                if(inv.getBoxId()!=null && inv.getBoxId()>0) {
                    savePath = inv.getDeviceIndex() + "->" + "[" + zoneMap.get(inv.getZone()) + "]" +
                            inv.getPanelName() + "->" + inv.getBoxColor() +
                            "->" + inv.getGridRow() + inv.getGridColumn();
                }else {
                    savePath = inv.getDeviceIndex() + "->" + "[" + zoneMap.get(inv.getZone()) + "]" +
                            inv.getPanelName();
                }
                //消息推送
                ReagentShareTemp reagentShareTemp = new ReagentShareTemp(userInfo.getLabId(), toLabMember.getUserId(), toLabMember.getId(), userInfo.getName(), savePath, inv.getReagentName());
                messageProducer.sendMessagePush(reagentShareTemp);
            });

        }
    }
}
